using System;
using System.Collections.Specialized;
using System.Data;
using System.Web;
using Org.EasyWeb.Constraint;
using Org.EasyWeb.Data;
using Org.EasyWeb.Exception;
using Org.EasyWeb.Sys;
using Org.EasyWeb.SysUtil;

namespace Org.EasyWeb.Right.Data
{
	public class LoginBaseDataSet: BaseDataSet,IXmlHttpPost2Page
	{
		private LoginMode fMode;
		private DataTable fLoginTable;
		private DataRow fLoginRow;
		private ErrorObjectCollection fErrorObjects;

		public LoginBaseDataSet(LoginMode mode)
		{
			fMode = mode;
			fErrorObjects = new ErrorObjectCollection("Login");
		}

		public string LoginName
		{
			get
			{
				return fLoginRow["LOGIN_NAME"].ToString();
			}
			set
			{
				fLoginRow["LOGIN_NAME"] = value;
			}
		}

		public string Password
		{
			get
			{
				return fLoginRow["LOGIN_PASS"].ToString();
			}
			set
			{
				fLoginRow["LOGIN_PASS"] = value;
			}
		}

		public int LoginAttempts
		{
			get
			{
				return int.Parse(fLoginRow["LOGIN_ATTEMPTS"].ToString());
			}
			set
			{
				fLoginRow["LOGIN_ATTEMPTS"] = value;
			}
		}

		public DataRow LoginRow
		{
			get
			{
				return fLoginRow;
			}
		}

		protected virtual string[] LoginTableFields
		{
			get
			{
				return new string[] { "LOGIN_MODE", "LOGIN_NAME", "LOGIN_PASS", "LOGIN_ATTEMPTS" };
			}
		}

		internal void CreateLoginTable()
		{
			fLoginTable = DataSetUtil.CreateDataTable("LOGIN", LoginTableFields); 
			Tables.Add(fLoginTable);
			fLoginRow = fLoginTable.NewRow();
			fLoginRow["LOGIN_MODE"] = fMode.ToString();
			fLoginRow["LOGIN_ATTEMPTS"] = 0;
			fLoginTable.Rows.Add(fLoginRow);
		}

		protected virtual void CheckLogin(DataRow row, IUserInfo info)
		{
		}

	

		public virtual void SetDefaultValue(HttpRequest request) 
		{
		}

        #region IXmlHttpPost2Page Members

        public string GetJScript(PageStyle style, string operation)
        {
            return string.Empty;
        }

        public string Post(PageStyle style, string operation, NameValueCollection queryString, DataSet postDataSet)
        {
            DataRow row = postDataSet.Tables["LOGIN"].Rows[0];
            try
            {
                CheckLogin(row, GlobalVariable.Info);
            }
            catch (LoginException ex)
            {
                DataSetCopyUtil.CopyRow(row, fLoginRow, LoginTableFields, LoginTableFields);
                ++LoginAttempts;
                if (ex is UserLockedException && LoginAttempts >= LoginUtil.GetPasswdLengthConfig())
                    LoginAttempts = 0;
                fErrorObjects.Add(ex.ErrorObject);
            }
            try
            {
                fErrorObjects.CheckError();
            }
            catch (InformationException ex)
            {
                Tables.Add(fErrorObjects.ErrorTable);
                throw ex;
            }	

            return string.Empty;
        }

        public string GetAlertString(PageStyle style, string operation, NameValueCollection queryString, DataSet postDataSet)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IXml2Page Members

        public string GetXsltFile(bool isIe, PageStyle style, string operation)
        {
            return string.Empty;
        }

        public string GetDefaultPage(bool isPost, PageStyle style, string operation, PageX pageX, string retURL)
        {
            return string.Empty;
        }

        #endregion

        #region IHttpGet2Page Members

        public void SetData(bool isPost, PageStyle style, string operation, NameValueCollection queryString)
        {
           
        }

        #endregion
    }
}
